package com.amap.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.amap.R;
import com.amap.banco.Banco;
import com.amap.model.Food;

public class FoodAdapter  extends BaseAdapter {

	private Context context;
	private List<Food> foods;

	private Cursor foodCursor;
	private int i;

	public FoodAdapter(Context context) {
		super();
		this.context = context;
		
		foods = new ArrayList<Food>();
		foodCursor = Banco.ConsultaPreferencias ();		
		foodCursor.moveToFirst();
		
		Food food;
		
		for(i=0;i < foodCursor.getCount(); i++){
			food = new Food(foodCursor.getString(0), foodCursor.getInt(1), foodCursor.getString(2));
			foods.add(food); 
			if(!foodCursor.isLast()){
				foodCursor.moveToNext();
			}
		}		
	}
	
	public void setFoods(List<Food> foods) {
		this.foods = foods;
	}

	@Override
	public int getCount() {
		return foods.size();
	}

	@Override
	public Object getItem(int position) {
		return foods.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Food food = foods.get(position);
		
        if (convertView == null) {
        	LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	convertView = inflater.inflate(R.layout.food_list, null);
        }
        
        TextView foodName = (TextView) convertView.findViewById(R.id.foodName);
        foodName.setText(food.getName());
        
		return convertView;
	}
	
	

}
